import {Component, OnInit} from '@angular/core';
import {B2bService} from '../b2b.service';
import {CommonService} from '../../../services-common/common.service';
import {NzModalService} from 'ng-zorro-antd/modal';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-order-manage',
  templateUrl: './order-manage.component.html',
  styleUrls: ['./order-manage.component.scss']
})
export class OrderManageComponent implements OnInit {
  query = {
    orderNum: '',
    payFlag: '',
    status: '',
    storeId: '',
    goods: '',
    name: '',
    page: 1,
    pageSize: 10,
  };
  tableData = {
    records: [],
    total: 100,
  };
  storeList = [];
  STATUS = ['待发货', '已发货', '已取消'];
  errImg = '';

  constructor(
    public b2bService: B2bService,
    public commonService: CommonService,
    public nzModalService: NzModalService,
    public nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.getOrderList();
    this.b2bService.getStoreList().then((res: any) => {
      if (res.code === 0) {
        this.storeList = res.data;
      }
    });
  }

  onPageIndexChange(index: number) {
    this.query.page = index;
    this.getOrderList();
  }

  onPageSizeChange(pageSize: number) {
    this.query.pageSize = pageSize;
    this.getOrderList();
  }

  getOrderList() {
    this.tableData.records = [];
    this.b2bService.getOrderList(this.query).then((res: any) => {
      if (res.code === 0) {
        this.tableData = res.data;
      }
    });
  }

  queryBtn() {
    this.query.page = 1;
    this.getOrderList();
  }

  resetCondition() {
    this.query = {
      orderNum: '',
      payFlag: '',
      status: '',
      storeId: '',
      goods: '',
      name: '',
      page: 1,
      pageSize: 10,
    };
    this.getOrderList();
  }

  export() {

    this.nzModalService.confirm({
      nzTitle: '提示',
      nzContent: '即将导出符合本页面筛选条件的订单数据，为了导出速度请尽可能控制导出范围。确定导出？',
      nzOkText: '确定',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: async () => {
        const res: any = await this.b2bService.exportOrder(
          {
            orderNum: this.query.orderNum,
            payFlag: this.query.payFlag,
            status: this.query.status,
            storeId: this.query.storeId,
            goods: this.query.goods,
            name: this.query.name
          }
        );
        if (res.type === 'application/octet-stream') {
          this.commonService.downLoadBlobFile(res, '订单信息');
        } else {
          this.nzMessageService.error('导出失败');
        }
      },
      nzCancelText: '关闭'
    });
  }
}
